package com.ctgu.simplespringboot.Controller;

import com.ctgu.simplespringboot.Entity.User;
import com.ctgu.simplespringboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;
    //开发用户注册
    @PostMapping("regist")
    public String regist(User user, String authCode,HttpSession session){
        if (session.getAttribute("authCode").toString().equalsIgnoreCase(authCode)){
            userService.save(user);
            return "redirect:/login.jsp";
        }else {
            return "redirect:/regist.jsp";
        }
    }

    //开发用户登录
    @RequestMapping("login")
    public String login(String username, String password, HttpSession session){

        User user = userService.login(username,password);
        //判断
        if (user!=null){
            session.setAttribute("user",user);
            return "redirect:/emp/findAll";  //查询员工的所有
        }else {
            return "redirect:/login.jsp";
        }
    }
}
